#ifndef DOWNLOADTHREAD_H
#define DOWNLOADTHREAD_H

/***************************************************************************
 * This file is part of the TTK Downloader project
 * Copyright (C) 2015 - 2025 Greedysky Studio

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License along
 * with this program; If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/

#include <QNetworkReply>
#include "downloadobject.h"
#include "ttkmoduleexport.h"

class QFile;

/*! @brief The class of the download thread.
 * @author Greedysky <greedysky@163.com>
 */
class TTK_MODULE_EXPORT DownloadThread : public QObject
{
    Q_OBJECT
public:
    struct Info
    {
        int m_index;
        QString m_url;
        QFile *m_file;
        qint64 m_startPoint;
        qint64 m_endPoint;
        qint64 m_readySize;
    };

public:
    /*!
     * Object constructor.
     */
    explicit DownloadThread(QObject *parent = nullptr);
    /*!
     * Object destructor.
     */
    ~DownloadThread();

    /*!
     * Start to download.
     */
    void startDownload(const Info &info);

    /*!
     * Get download url.
     */
    inline QString url() const noexcept { return m_info.m_url; }
    /*!
     * Get ready size.
     */
    inline qint64 readySize() const noexcept { return m_info.m_readySize; }
    /*!
     * Get start point(.
     */
    inline qint64 startPoint() const noexcept { return m_info.m_startPoint; }
    /*!
     * Get end point.
     */
    inline qint64 endPoint() const noexcept { return m_info.m_endPoint; }

Q_SIGNALS:
    /*!
     * Download finished.
     */
    void finished(int index);
    /*!
     * Show error code index and string.
     */
    void errorOccurred(int index, const QString &error);
    /*!
     * Download state changed.
     */
    void downloadChanged();

public Q_SLOTS:
    /*!
     * Pause to download.
     */
    void pause();
    /*!
     * Restart to download.
     */
    void restart();

private Q_SLOTS:
    /*!
     * Download data from net finished.
     */
    void downLoadFinished();
    /*!
     * Download received data ready.
     */
    void handleReadyRead();
    /*!
     * Download reply error.
     */
    void handleError(QNetworkReply::NetworkError code);

private:
    Info m_info;
    QNetworkAccessManager *m_manager;
    QNetworkReply *m_reply;
    TTK::DownloadState m_state;

};

#endif // DOWNLOADTHREAD_H
